#include "eggclock.h"

eggclock::eggclock(QWidget *parent) :
    QWidget(parent)
{
    setFixedSize(220,100);

    secs = 30;
    mins = 2;
    minutes = new QLineEdit(QString::number(mins),this);
    seconds = new QLineEdit(QString::number(secs),this);
    startButton = new QPushButton("Start",this);
    layout = new QHBoxLayout;
    timer = new QTimer(this);
    msgBox = new QMessageBox();
    label = new QLabel(":");

    menu = new QMenuBar();
    fileMenu = new QMenu("File",this);
    presetsMenu = new QMenu("Presets",this);

    quitAction = new QAction("Quit",this);
    ceylonAction = new QAction("Ceylon S&&M",this);
    fileMenu->addAction(quitAction);
    presetsMenu->addAction(ceylonAction);
    menu->addMenu(fileMenu);
    menu->addMenu(presetsMenu);

    presets[0].name = "ceylon assam";
    pw = new presetWindow();

    minutes->setMaxLength(2);
    seconds->setMaxLength(2);
    minutes->setFont(QFont("Times",18,QFont::Bold));
    label->setFont(QFont("Times",18,QFont::Bold));
    seconds->setFont(QFont("Times",18,QFont::Bold));
    startButton->setFont(QFont("Times",18,QFont::Bold));

    layout->setMenuBar(menu);
    layout->addWidget(minutes);
    layout->addWidget(label);
    layout->addWidget(seconds);
    layout->addWidget(startButton);

    setLayout(layout);

    this->setWindowTitle("Eggclock");

    connect(timer, SIGNAL(timeout()),this,SLOT(updateTime()));
    connect(minutes,SIGNAL(textChanged(QString)),this,SLOT(updateInputs()));
    connect(seconds,SIGNAL(textChanged(QString)),this,SLOT(updateInputs()));
    connect(startButton,SIGNAL(clicked()),this,SLOT(startTimer()));
    connect(quitAction, SIGNAL(triggered()),qApp,SLOT(quit()));
    connect(ceylonAction,SIGNAL(triggered()),this,SLOT(openPresetWindow()));
}

void eggclock::updateInputs()
{
    if (eggclock::isNumeric(minutes->text()) == true)
    {
        mins = minutes->text().toInt();
    }
    else
    {
        minutes->setText(QString::number(mins));
    }

    if (eggclock::isNumeric(seconds->text()) == true)
    {
        secs = seconds->text().toInt();
    }
    else
    {
        if (secs < 10)
        {
            seconds->setText("0" + QString::number(secs));
        }
        else
        {
            seconds->setText(QString::number(secs));
        }

    }
}

bool eggclock::isNumeric(QString str)
{
    foreach (QChar c, str)
    {
        if (c.isNumber() == false)
        {
            return false;
        }
    }
    return true;
}

void eggclock::updateTime()
{
    if (secs > 0)
    {
        secs--;
        if (secs < 10)
        {
            seconds->setText("0" + QString::number(secs));
        }
        else
        {
            seconds->setText(QString::number(secs));
        }

    }

    else
        if (secs == 0 && mins > 0)
        {
            mins--;
            minutes->setText(QString::number(mins));
            secs = 59;
            seconds->setText(QString::number(secs));
        }
        else
        {
            msgBox->setText("Hoplaa!");
            msgBox->exec();
            //QApplication::alert();
            timer->stop();
            minutes->setEnabled(true);
            seconds->setEnabled(true);
            startButton->setEnabled(true);
            started = false;
            startButton->setText("Start");
        }
        this->setWindowTitle("Eggclock (" + minutes->text() + ":" + seconds->text() + ")");
}

void eggclock::startTimer()
{
    if (started == false)
    {
        started = true;
        timer->start(1000);
        startButton->setText("Stop");
        minutes->setEnabled(false);
        seconds->setEnabled(false);
    }
    else
    {
        started = false;
        timer->stop();
        startButton->setText("Start");
        minutes->setEnabled(true);
        seconds->setEnabled(true);
    }

}

void eggclock::openPresetWindow()
{
    pw->show();
}
